package com.sojson.util.id.impl;

import java.security.SecureRandom;

import com.sojson.util.DateUtil;

/**
 * 编号工具类
 *
 * @author lyh
 * 2020/11/26 15:59
 */
public class OrderNoUtilImpl {

    public OrderNoUtilImpl() {}
    // 机房编号(3位)
    private final String MACHINE_ROOM = "001";
    // 机器编号(4位)
    private final String MACHINE = "0001";
    // 4位随机字符
    private final String PROJECT = MACHINE_ROOM + MACHINE;
    // 随机数对象
    private final SecureRandom RANDOM = new SecureRandom();
    // 4位随机字符
    private final String RANDOM_CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public String generate(String key) {
        return key + nowString() + PROJECT + new String(chars(4, RANDOM_CHAR));
    }

    /**
     * 随机码
     */
    private char[] chars(int n, String base) {
        char[] chars = new char[n];
        for (int index = 0; index < chars.length; ++index) {
            chars[index] = base.charAt(RANDOM.nextInt(base.length()));
        }
        return chars;
    }

    /**
     * 解密方法
     * 
     * @param inStr
     * @return
     */
    private String nowString() {
        return DateUtil.nowString(DateUtil.YYYYMMDDHHMMSS);
    }

}